import { ref } from 'vue'
import { defineStore } from 'pinia'
import { fetchRegionFromIp } from "@/service/city";
export const useLocation = defineStore('location', () => {
  const location = ref({
    lat: 0,
    lon: 0,
    cityName: '',
    cityCode: '',
  })
  async function  getCity() {
    const res = await fetchRegionFromIp()
    location.value.cityName = res.data.name
    location.value.cityCode = res.data.code || res.data.id
  }
  async function updateLocation() {
    // const { data = [] } = await getCart()
    location.value.lat = 23.12908
    location.value.lon = 113.26436
  }

  async function updateCity(cityCode: string, cityName: string) {
    location.value.cityCode = cityCode
    location.value.cityName = cityName
  }


  return { location, updateLocation, getCity, updateCity }
})
